package com.xuxin.baby.network;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.xuxin.baby.bean.BaseBean;

import java.io.IOException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import retrofit2.Response;

/**
 * Created by xu_xin
 * Created on 2020/12/7 0007
 */
public abstract class myObserver<T> extends DisposableObserver<Response<T>> {
    @Override
    public void onNext(@NonNull Response<T> response) {
        if (response.code() == 200 || response.code() == 201 ||
                response.code() == 202 || response.code() == 203 ||
                response.code() == 204 || response.code() == 205) {
            onNextX(response.body());
        } else if (null != response.errorBody()) {
            try {
                String message = response.errorBody().string();
                BaseBean baseBean = GsonUtils.fromJson(message, BaseBean.class);
                baseBean.setSuccess(false);
                if (response.code() == 401) {
                    baseBean.setUnAuthorizedRequest(true);
                }
                onError(baseBean);
            } catch (IOException e) {
                e.printStackTrace();
                onError(new BaseBean());
            }
        } else {
            onError(new BaseBean());
        }
    }

    public abstract void onNextX(T t);

    public abstract void onError(BaseBean baseBean);
}